﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Drawing;
using System.Net;
using System.Web;
using System.IO;
using System.Threading;


namespace Allocine_Lib
{
    public class Film : INotifyPropertyChanged

    {
        #region Member Fields


        private string m_FilmId;

        public string ID
        {
            get { return m_FilmId; }
            set { m_FilmId = value; }
        }

        private string m_FilmTitle;

        public string Titre
        {
            get { return m_FilmTitle; }
            set { m_FilmTitle = value; }
        }

        private string m_FilmAnnee;

        public string Annee
        {
            get { return m_FilmAnnee; }
            set { m_FilmAnnee = value; }
        }

        private Image m_Thumbnail;

        public Image Thumbnail
        {
            get { return m_Thumbnail; }
            set { m_Thumbnail = value; }
        }

        private DateTime m_FilmDateSortie;

        public DateTime DateSortie
        {
            get { return m_FilmDateSortie; }
            set { m_FilmDateSortie = value; }
        }

        private string m_FilmOriginalTitle;

        public string TitreOriginal
        {
            get { return m_FilmOriginalTitle; }
            set { m_FilmOriginalTitle = value; }
        }

        private string[] m_FilmAuthors;

        public string[] Realisateurs
        {
            get { return m_FilmAuthors; }
            set { m_FilmAuthors = value; }
        }

        private string[] m_FilmActors;

        public string[] Acteurs
        {
            get { return m_FilmActors; }
            set { m_FilmActors = value; }
        }

        private string m_FilmSynopsis;

        public string Synopsis
        {
            get { return m_FilmSynopsis; }
            set { m_FilmSynopsis = value; }
        }

        private int m_FilmDureeMin;

        public int DureeMin
        {
            get { return m_FilmDureeMin; }
            set { m_FilmDureeMin = value; }
        }

        private string m_FilmDureeChaine;

        public string DureeChaine
        {
            get { return m_FilmDureeChaine; }
            set { m_FilmDureeChaine = value; }
        }


        private Image m_FilmImage;

        public Image Image
        {
            get { return m_FilmImage; }
            set { m_FilmImage = value; }
        }

        private string m_FilmURLImage;

        public string URLImage
        {
            get { return m_FilmURLImage; }
            set { m_FilmURLImage = value; }
        }

        private string m_FilmURLBandeAnnonce;

        public string URLBandeAnnonce
        {
            get { return m_FilmURLBandeAnnonce; }
            set { m_FilmURLBandeAnnonce = value; }
        }


        private string[] m_FilmGenre;

        public string[] Genre
        {
            get { return m_FilmGenre; }
            set { m_FilmGenre = value; }
        }

        private float m_FilmNotePress;

        public float NotePresse
        {
            get { return m_FilmNotePress; }
            set { m_FilmNotePress = value; }
        }

        private float m_FilmNoteSpectateurs;

        public float NoteSpectateurs
        {
            get { return m_FilmNoteSpectateurs; }
            set { m_FilmNoteSpectateurs = value; }
        }

        #endregion

        public Film()
        {
            this.m_Thumbnail = Allocine_Lib.Properties.Resources.Thumbnail_Default;
            this.m_FilmImage = Allocine_Lib.Properties.Resources.loading2;

        }

        public event PropertyChangedEventHandler PropertyChanged;


        public void LoadThumbnail(string _path)
        {
            try
            {
                WebClient client = new WebClient();
 
                WebProxy wProxy = new WebProxy("10.126.71.12", 80);
                wProxy.Credentials = new NetworkCredential("rfraftp", "Siberbo2000", "fr");
                client.Proxy = wProxy;

                client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(Thumbnail_DownloadDataCompleted);
                client.DownloadDataAsync(new Uri(_path));

            }
            catch (Exception ex)
            {
                //Log.Error("Error while loading image ", ex);
                //
            }
        }

        public void LoadImage(string _path)
        {
            try
            {
                WebClient client = new WebClient();

                WebProxy wProxy = new WebProxy("10.126.71.12", 80);
                wProxy.Credentials = new NetworkCredential("rfraftp", "Siberbo2000", "fr");
                client.Proxy = wProxy;

                client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(Image_DownloadDataCompleted);
                client.DownloadDataAsync(new Uri(_path));

            }
            catch (Exception ex)
            {
                //Log.Error("Error while loading image ", ex);
                //
            }
        }

        private void Thumbnail_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {

            try
            {
                byte[] result = e.Result;
                MemoryStream ms = new MemoryStream(result);
                this.m_Thumbnail = Image.FromStream(ms, true, true);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Thumbnail"));
                }
            }
            catch (Exception)
            {
                
                //throw;
            }

        }

        private void Image_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {

            try
            {
                byte[] result = e.Result;
                MemoryStream ms = new MemoryStream(result);
                this.m_FilmImage = Image.FromStream(ms, true, true);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Image"));
                }
            }
            catch (Exception)
            {
                
                //throw;
            }

        }
  
    
    }
   
    public class Films : ObservableCollection<Film>
    {
        
        //
    }


    public class FilmComparer : IComparer<Film>
    {

        public int Compare(Film _Film1, Film _Film2)
        {
            int i = -1;
            i = _Film1.Titre.CompareTo(_Film2.Titre);
            return i;
        }

    }

    

}
